package com.easyjava.vuebuilder;

import com.easyjava.bean.Constants;
import com.easyjava.builder.BuildBase;
import com.easyjava.vuebean.ConstantsVue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 小闹心
 * @Description: 生成基础类
 * @DateTime: 2024/11/21 10:36
 **/
public class BuilderVueBase {
    private static final Logger logger = LoggerFactory.getLogger(BuilderVueBase.class);
    public static void execute(){
        build("httpUtils", ConstantsVue.PATH_VUE_UTILS);

        List<String> contentList = new ArrayList<>();
        contentList.add("// 后端");
        contentList.add("export const serverUrl = '" + ConstantsVue.VUE_SERVER_URL +"'");
        BuilderVueCommon.execute("common",ConstantsVue.PATH_VUE_UTILS,contentList);
    }
    private static void build(String fileName,String outPutPath){
        File folder = new File(outPutPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        File javaFile = new File(folder,fileName + ".js");

        OutputStream out = null;
        OutputStreamWriter outWriter = null;
        BufferedWriter bw = null;

        InputStream in = null;
        InputStreamReader inReader = null;
        BufferedReader br = null;

        try{
            out = new FileOutputStream(javaFile);
            outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outWriter);

            String templatePath = BuildBase.class.getClassLoader().getResource("template/vue/" + fileName + ".txt").getPath();
            in = new FileInputStream(templatePath);
            inReader = new InputStreamReader(in, StandardCharsets.UTF_8);
            br = new BufferedReader(inReader);
            String lineInfo = null;
            while((lineInfo = br.readLine()) != null){
                bw.write(lineInfo);
                bw.newLine();
            }
            bw.flush();
        }catch (Exception e){
            logger.error("生成前端基础类: {} 失败" ,fileName);
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inReader != null){
                try {
                    inReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outWriter != null){
                try {
                    outWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
